//
//  custCollectCell.m
//  xinhua_sx
//
//  Created by love on 14-8-17.
//  Copyright (c) 2014年 dangyangyang. All rights reserved.
//

#import "custCollectCell.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"

@implementation custCollectCell
@synthesize imageView;
@synthesize webView;
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 80, 120)];
        self.imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.backgroundColor = [UIColor blackColor];
        [self addSubview:self.imageView];
        self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, 80, 120)];
        //self.webView.contentMode = UIViewContentModeScaleToFill;
        self.webView.scrollView.scrollEnabled = NO;
        //self.webView.scalesPageToFit = YES;
        [self.webView sizeToFit];
        //imageView.backgroundColor = [UIColor blackColor];
        //[self addSubview:self.webView];
    
    }
    return self;
}
-(void)setDic:(id)dic
{
    NSString* st = [NSString stringWithFormat:@"http://183.203.18.45:7001%@",[dic objectForKey:@"path"]];
    //[imageView setImageURLStr:st placeholder:[UIImage imageNamed:@"base_detail_default_pic.png"]];
   // [self.imageView setImageWithURL:[NSURL URLWithString:st] placeholderImage:[UIImage imageNamed:@"base_detail_default_pic.png"]];
    //[self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:st]]];
    //[self.imageView setImageWithURL:[NSURL URLWithString:st] placeholderImage:[UIImage imageNamed:@"base_detail_default_pic.png"]];
    //[self.imageView setImageWithURL:[NSURL URLWithString:@"http://www.fzlol.com/upimg/allimg/120819/2021144O91.jpg"] placeholderImage:[UIImage imageNamed:@"有种想念叫做避而不见.jpg"] success:^(UIImage *image){} failure:^(NSError *error){} ];
    GDRSImageCache *cache = [[GDRSImageCache alloc] initWithCachedImageFilter:^UIImage *(UIImage *sourceImage) {
        
        // resize the image to the image view size and round the image corners;
        // this is called by the cache on a background thread
        return [sourceImage gdrs_resizedImageToAspectFitSize:self.imageView.bounds.size cornerRadius:10];
        
    }];
    
    // set the default image, which will be returned from
    // fetchImageWithURL:completionHandler: if an image coresponding to the
    // requested url is not cached yet.
    cache.defaultImage = [UIImage imageNamed:@"base_detail_default_pic.png"];
    
    
    NSURL *imageUrl = [NSURL URLWithString:st];
    
    // fetch an image; the call returns imidiatly and the callback handler
    // is called when the image is fetched over the network
    self.imageView.image = [cache fetchImageWithURL:imageUrl completionHandler:^(UIImage *image, NSError *error) {
        self.imageView.image = image;
    }];
    /*[self.imageView setImageWithURL:[NSURL URLWithString:st]
                   placeholderImage:[UIImage imageNamed:@"base_detail_default_pic.png"]
                          completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {
                              NSLog(@"下载完成");
                              if (error == nil)
                              {
                                  NSLog(@"下载完成1");
                                  //image = [custCollectCell thumbnailWithImage:image size:CGSizeMake(80, 120)];
                                 // cacheType = SDImageCacheTypeNone;
                                  //[[SDImageCache sharedImageCache] clearDisk];
                                  
                                  [[SDImageCache sharedImageCache] clearMemory];
                              }
                              else
                              {
                                  NSLog(@"errol = %@",error);
                              }
                          }];*/
    
   
   
       
}



-(void)dealloc

{
    NSLog(@"iamge dell");
}

//1.自动缩放到指定大小

+ (UIImage *)thumbnailWithImage:(UIImage *)image size:(CGSize)asize

{
    
    UIImage *newimage;
    
    
    if (nil == image) {
        
        newimage = nil;
        
    }
    
    else{
        
        UIGraphicsBeginImageContext(asize);
        
        [image drawInRect:CGRectMake(0, 0, asize.width, asize.height)];
        
        newimage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
    }
    
    return newimage;
    
}

//2.保持原来的长宽比，生成一个缩略图

+ (UIImage *)thumbnailWithImageWithoutScale:(UIImage *)image size:(CGSize)asize

{
    
    UIImage *newimage;
    
    
    if (nil == image) {
        
        newimage = nil;
        
    }
    
    else{
        
        CGSize oldsize = image.size;
        
        CGRect rect;
        
        if (asize.width/asize.height > oldsize.width/oldsize.height) {
            
            rect.size.width = asize.height*oldsize.width/oldsize.height;
            
            rect.size.height = asize.height;
            
            rect.origin.x = (asize.width - rect.size.width)/2;
            
            rect.origin.y = 0;
            
        }
        
        else{
            
            rect.size.width = asize.width;
            
            rect.size.height = asize.width*oldsize.height/oldsize.width;
            
            rect.origin.x = 0;
            
            rect.origin.y = (asize.height - rect.size.height)/2;
            
        }
        
        
        UIGraphicsBeginImageContext(asize);
        
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
        
        UIRectFill(CGRectMake(0, 0, asize.width, asize.height));//clear background
        
        [image drawInRect:rect];
        
        newimage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
    }
    
    return newimage;
    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
